package com.lebang.component;

import com.lebang.common.util.AmountUtil;
import com.lebang.entity.member.LbbMemberRecord;
import com.lebang.entity.task.LbbCommon;
import com.lebang.entity.task.LbbSosoWithBLOBs;
import com.lebang.example.LbbCommonExample;
import com.lebang.example.LbbSosoExample;
import com.lebang.mapper.LbbCommonMapper;
import com.lebang.mapper.LbbMemberRecordMapper;
import com.lebang.mapper.LbbSosoMapper;
import com.lebang.mapper.buss.multiword.MultiwordSpeechMapper;
import com.lebang.mapper.buss.multiword.MultiwordSpeechPlanMapper;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.model.enums.buss.TaskStatus;
import com.lebang.model.vo.buss.TaskSearchVo;
import com.lebang.model.vo.buss.multiword.MultiwordSpeech;
import com.lebang.model.vo.buss.multiword.MultiwordSpeechPlan;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.buss.MemberRecordService;
import com.lebang.service.buss.WalletService;
import com.lebangweb.common.DateUtil;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: hwq
 * @Date: 2019/6/11 15:58
 * @Version 1.0
 */
@Component
public class CommonComponent {

    public static final String TASK_COMMON_AMOUNT = "task_common_amount";
    private static final Logger log = LoggerFactory.getLogger(CommonComponent.class);
    @Autowired
    private MultiwordSpeechPlanMapper multiwordSpeechPlanMapper;
    @Autowired
    private MultiwordSpeechMapper multiwordSpeechMapper;
    @Autowired
    private WalletService walletService;

    @Autowired
    private RedisClient redisClient;
    @Autowired
    private MemberRecordService memberRecordService;
    @Autowired
    private LbbMemberRecordMapper lbbMemberRecordMapper;
    @Autowired
    private LbbCommonMapper lbbCommonMapper;
    @Autowired
    private MemberNoticeService memberNoticeService;
    @Resource
    private LbbSosoMapper sosoMapper;

    public Map<String, Object> pagging(TaskSearchVo vo, Integer status) {
        Integer total = lbbCommonMapper.total(vo, status);
        List<Map<String, Object>> rows = lbbCommonMapper.pagging(vo, status);
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("total", total);
        data.put("rows", rows);
        return data;
    }


    public List<Map<String, Object>> task(Integer status) {

        return lbbCommonMapper.task(status);
    }

    public void delDate() {
        lbbCommonMapper.delDate();
    }


    public JsonData checkList(List<String> list, String tab, String status, String remark) {
        String uuid = UUIDUtil.M();
        boolean lock = redisClient.tryGetDistributedLock("lock:common", uuid, 30000);
        try {
            if (!lock) {
                return Callback.fail("200", "操作过于频繁!", null);
            }
            List<LbbMemberRecord> recordList = lbbMemberRecordMapper.findCheckListByid(list);
            for (LbbMemberRecord record : recordList) {
                if (record.getStatus().equals(TaskStatus.CheckSuccess)) {
                    continue;
                }
                record.setStatus(TaskStatus.getByTitle(status));
                LbbCommon task = lbbCommonMapper.getByTaskId(record.getTaskId());
                record.setStatus(TaskStatus.getByTitle(status));
                if (record.getStatus().equals(TaskStatus.CheckFailure)) {
                    record.setRemark(remark);
                }
                record.setCheckTime(new Date());
                memberRecordService.updateByPrimaryKeySelective(record);
                if (record.getStatus().equals(TaskStatus.CheckSuccess)) {
                    // 计算奖励
                    Integer amount ;
                    if(record.getUserMoney().equals(0)){
                        amount = AmountUtil.checkAmount(task);
                    }else {
                        amount=record.getUserMoney();
                    }
                    if (task.getIsMultiword() == 1) {
                        MultiwordSpeech multiwordSpeech = multiwordSpeechMapper.selectByPrimaryKey(Long.parseLong(record.getOrderId()));
                        int i1 = 0;
                        int o = 0;
                        if (multiwordSpeech.getHasPlan() == 1) {

                            String str = DateUtil.dateToString(record.getCreateTime(), "yyyyMMdd");
                            MultiwordSpeechPlan plan = multiwordSpeechPlanMapper.getmultiwordSpeech(multiwordSpeech.getId(), str);
                            if (plan != null) {
                                plan.setTadayTotal(plan.getTadayTotal() + 1);
                                o = multiwordSpeechPlanMapper.updateByPrimaryKeySelective(plan);
                            }
                            multiwordSpeech.setAllTotal(multiwordSpeech.getAllTotal() + 1);
                            multiwordSpeech.setTadayTotal(multiwordSpeech.getTadayTotal() + 1);
                            i1 = multiwordSpeechMapper.updateByPrimaryKeySelective(multiwordSpeech);
                        } else {
                            o = 1;
                            multiwordSpeech.setAllTotal(multiwordSpeech.getAllTotal() + 1);
                            multiwordSpeech.setTadayTotal(multiwordSpeech.getTadayTotal() + 1);
                            i1 = multiwordSpeechMapper.updateByPrimaryKeySelective(multiwordSpeech);
                        }
                    }

                    walletService.income(record.getMemberId(), record, TaskCategory.Common, record.getTaskId(), task.getTitle(), amount);
                    //memberNoticeService.notice(record, task.getTitle(), "任务通知:提交的任务审核成功!");
                } else {
                    memberNoticeService.notice(record, task.getTitle(), "对不起，您提交的不符合要求，审核失败!");
                }
            }
            return Callback.success("200", "批量审核成功", null);
        } catch (Exception e) {
            e.printStackTrace();

            return Callback.fail("200", "批量审核失败", e);
        } finally {
            redisClient.releaseDistributedLock("lock:common", uuid);
        }
    }


    public Map<String, Object> speechPaging(Integer id, Integer page, Integer limit) {
        Integer total = lbbCommonMapper.speechtotal(id);
        List<Map<String, Object>> rows = lbbCommonMapper.speechpagging(id, page, limit);
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("total", total);
        data.put("rows", rows);
        return data;
    }

    public Map<String, Object> speechPagingPlan(Integer id, Integer page, Integer limit) {
        Integer total = lbbCommonMapper.speechplantotal(id);
        List<Map<String, Object>> rows = lbbCommonMapper.speechplanpagging(id, page, limit);
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("total", total);
        data.put("rows", rows);
        return data;
    }
    public List<LbbCommon> all() {
        return lbbCommonMapper.selectByExample(new LbbCommonExample());
    }



    public void release(List<String> list) {
        lbbCommonMapper.releaseList(list);
    }



    public List<LbbSosoWithBLOBs> sosoall() {
        return sosoMapper.selectByExampleWithBLOBs(new LbbSosoExample());
    }

    public List<Map<String, Object>> selectCommon(String title, String status, int offset, int limit) {
        return lbbCommonMapper.selectCommon(title,status,offset,limit);
    }

    public long countComon(String title, String status) {
        return lbbCommonMapper.countComon(title,status);
    }
}
